package com.jxm.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxm.security.entity.User;
import com.jxm.security.mapper.PermissionMapper;
import com.jxm.security.mapper.UserMapper;
import com.jxm.security.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/5/24 21:53
 * @Version: 1.0
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //根据用户名查询用户信息
        User selectOne = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if(selectOne == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        //用户权限信息
        ArrayList<String> permissions = permissionMapper.getPermissinByUserId(selectOne.getId());

        SysUser sysUser = new SysUser(selectOne,permissions);

        return sysUser;
    }
}
